﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Model;

namespace LaKuaShop_TestDemo.Page
{
    /// <summary>
    /// RegisterPage.xaml 的交互逻辑
    /// </summary>
    public partial class RegisterPage : System.Windows.Controls.Page
    {
        public RegisterPage()
        {
            InitializeComponent();
        }
        //进入注册按钮 弹出提示框
        private void Register_Click(object sender, RoutedEventArgs e)
        {
            string account = Username.Text;
            string password = Pass.Password;
            string passakg = Passakg.Password;
            if (account == "" || passakg == "" || password == ""
                || (male.IsChecked == false && female.IsChecked == false))
            {
                ErrMsg.IsActive = false;
                ExistMsg.IsActive = false;
                SuccessMsg.IsActive = false;
                ERRORMsg.IsActive = false;
                NullMsg.IsActive = true;
                PlenMsg.IsActive = false;
                return;
            }
            string sex = (male.IsChecked == true) ? "男" : "女";
            if (password != passakg)
            {

                NullMsg.IsActive = false;
                ExistMsg.IsActive = false;
                SuccessMsg.IsActive = false;
                ERRORMsg.IsActive = false;
                PlenMsg.IsActive = false;
                ErrMsg.IsActive = true;
                return;
            }
            else if (password.Length <= 6)
            {
                NullMsg.IsActive = false;
                ExistMsg.IsActive = false;
                SuccessMsg.IsActive = false;
                ERRORMsg.IsActive = false;
                ErrMsg.IsActive = false;
                PlenMsg.IsActive = true;
                return;
            }
            else
            {
                BLL.User User = new BLL.User();
                BLL.BuyCart BuyCart = new BLL.BuyCart();
                BLL.Collect Collect = new BLL.Collect();
                if (User.CheckAct(account) == false)
                {
                    NullMsg.IsActive = false;
                    ErrMsg.IsActive = false;
                    SuccessMsg.IsActive = false;
                    ERRORMsg.IsActive = false;
                    PlenMsg.IsActive = false;
                    ExistMsg.IsActive = true;
                    return;
                }
                else
                {
                    UserInfo user = new UserInfo(account, password, sex);
                    if ((User.CreateUserAct(user) == 0) && (BuyCart.AddBuyCart(account) == 0) && (Collect.AddCollect(account) == 0))
                    {
                        ;
                        NullMsg.IsActive = false;
                        ErrMsg.IsActive = false;
                        ExistMsg.IsActive = false;
                        ERRORMsg.IsActive = false;
                        PlenMsg.IsActive = false;
                        SuccessMsg.IsActive = true;
                        Username.Text = string.Empty;
                        Pass.Password = string.Empty;
                        Passakg.Password = string.Empty;
                        return;
                    }
                    else
                    {
                        User.DeleteUserInfo(account);
                        BuyCart.DelBuyCart(account);
                        Collect.DelCollect(account);
                        NullMsg.IsActive = false;
                        ErrMsg.IsActive = false;
                        ExistMsg.IsActive = false;
                        SuccessMsg.IsActive = false;
                        PlenMsg.IsActive = false;
                        ERRORMsg.IsActive = true;
                        Username.Text = string.Empty;
                        Pass.Password = string.Empty;
                        Passakg.Password = string.Empty;
                        return;
                    }
                }
            }
        }
    }
}
